//
//  NRWCarInputNumView.m
//  NRWMall
//
//  Created by 张记 on 2018/5/23.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWCarInputNumView.h"
#import "NRWAskingPirceApi.h"
@interface NRWCarInputNumView()<UITextFieldDelegate,YTKRequestDelegate>
@property (weak, nonatomic) IBOutlet UITextField *numTextF;
@property (weak, nonatomic) IBOutlet UIButton *submitB;
@property (nonatomic, copy) NSString *phoneNum;
@property (nonatomic, strong) NRWAskingPirceApi *askingPriceApi;
@end

@implementation NRWCarInputNumView

-(NRWAskingPirceApi *)askingPriceApi{
    if (!_askingPriceApi) {
        _askingPriceApi = [[NRWAskingPirceApi alloc] init];
        _askingPriceApi.delegate = self;
    }
    return _askingPriceApi;
}

+ (instancetype)InputNumView{
    NRWCarInputNumView *view = [[UINib nibWithNibName:@"NRWCarInputNumView" bundle:nil] instantiateWithOwner:nil options:nil].lastObject;
    
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH() - 32, 180);
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    
    return view;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.submitB.layer.masksToBounds = YES;
    self.submitB.layer.cornerRadius = 17.5;
    self.numTextF.layer.masksToBounds = YES;
    self.numTextF.layer.borderColor = getColor(@"999999").CGColor;
    self.numTextF.layer.borderWidth = 0.5f;
    self.numTextF.tintColor = mainColor();
    self.numTextF.delegate = self;
    self.numTextF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 0)];
    self.numTextF.leftView.userInteractionEnabled = NO;
    self.numTextF.leftViewMode = UITextFieldViewModeAlways;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.phoneNum = text;
    return YES;
}

- (IBAction)submitPhoneNum:(UIButton *)sender {
    if (isEmptyString(self.phoneNum) == YES) {
        [NRWMessageHUD showMessage:@"请输入手机号" forTime:2.0 withRemindId:remindWarning];
        return;
    }
    if ([DataCheck valiMobile:self.phoneNum] == NO) {
        [NRWMessageHUD showMessage:@"请输入正确手机号" forTime:2.0 withRemindId:remindWarning];
        return;
    }
    self.askingPriceApi.result = self.carModel;
    self.askingPriceApi.phoneNum = self.phoneNum;
    [self.askingPriceApi start];
    
}
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    if (request == self.askingPriceApi) {
     
        if ([request.responseObject[@"code"] isEqual:@1]){
            [NRWMessageHUD showMessage:@"提交成功" forTime:2.0 withRemindId:remindSuccess];

        }else{
            [NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:2.0 withRemindId:remindFail];
        }
    }
    self.clickBlock();
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [NRWMessageHUD showMessage:@"提交失败" forTime:2.0 withRemindId:remindFail];
    self.clickBlock();

}

@end
